from PyQt5 import QtWidgets
from PyQt5 import QtGui
import sys

class QTextEditTest(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(QTextEditTest, self).__init__(parent)

        self.setWindowTitle("QTextEdit例子")

        self.resize(300, 270)

        self.text_edit = QtWidgets.QTextEdit()

        self.button_show_text = QtWidgets.QPushButton("显示文本")
        self.button_show_text.clicked.connect(self.button_show_text_clicked)

        self.button_show_html = QtWidgets.QPushButton("显示Html")
        self.button_show_html.clicked.connect(self.button_show_html_clicked)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.text_edit)
        layout.addWidget(self.button_show_text)
        layout.addWidget(self.button_show_html)

        self.setLayout(layout)

    def button_show_text_clicked(self):
        self.text_edit.setPlainText("Hello PyQt5 !\n点击按钮")

    def button_show_html_clicked(self):
        self.text_edit.setHtml('''<font color='red' size='6'>Hello PyQt5 !\n点击按钮</font>''')

if __name__ == "__main__":
    q_app = QtWidgets.QApplication(sys.argv)
    win = QTextEditTest()
    win.show()
    sys.exit(q_app.exec_())